// pages/Withdrawals/Withdrawals.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardnumber: '',
    name: '',
    amount: '',
    array: [
      { bank_name: '请点击选择' },
      { bank_name: '中国工商银行', abbreviation: 'ICBC' },
      { bank_name: '农业银行', abbreviation: 'ABC' },
      { bank_name: '中国银行', abbreviation: 'BOC' },
      { bank_name: '建设银行', abbreviation: 'CCB' },
      { bank_name: '招商银行', abbreviation: 'CMB' },
      { bank_name: '邮政储蓄银行', abbreviation: 'PSBC' },
      { bank_name: '交通银行', abbreviation: 'BOCOM' },
      { bank_name: '浦发银行', abbreviation: 'SPDB' },
      { bank_name: '民生银行', abbreviation: 'CMBC' },
      { bank_name: '兴业银行', abbreviation: 'CIB' },
      { bank_name: '平安银行', abbreviation: 'PAB' },
      { bank_name: '中信银行', abbreviation: 'CITIC' },
      { bank_name: '华夏银行', abbreviation: 'HB' },
      { bank_name: '广发银行', abbreviation: 'GDB' },
      { bank_name: '光大银行', abbreviation: 'CEB' },
      { bank_name: '北京银行', abbreviation: 'BOB' },
      { bank_name: '宁波银行', abbreviation: 'BON' }
    ],
    index: 0,
    err:false,
    message:''
  },
  setTips: function (message){
    var that = this
    this.setData({
      err:true,
      message: message
    })
    setTimeout(function(){
      that.setData({
        err: false,
        message: ''
      })
    },2000)
  },
  getvalue: function (e) {
    var state = e.currentTarget.dataset.type
    var val = e.detail.value
    if (state == 0) {
      this.setData({
        name: val
      })
    }
    if (state == 1) {
      this.setData({
        cardnumber: val
      })
    }
    if (state == 2) {
      this.setData({
        amount: val * 100
      })
    }
  },
  confirm: function () {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.data_url + '/GPD/withdraw.do',
      data: {
        cardnumber: that.data.cardnumber,
        name: that.data.name,
        amount: that.data.amount,
        bankname: that.data.array[that.data.index].abbreviation,
        session_id: app.globalData.sessionId
      },
      success: function (res) {
        wx.hideLoading()
        if (res.data.result_code == 'SUCCESS'){
          wx.showToast({
            title: res.data.message,
          })
          setTimeout(function(){
            wx.navigateBack({
              delta:1
            })
          },1500)
        }else{
          that.setTips(res.data.message)
        }
      }
    })
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.data_url + '/GPD/getBankCard.do',
      data: {
        session_id: app.globalData.sessionId
      },
      success: function (res) {
        wx.hideLoading()
        let index = 0
        that.data.array.forEach(function (item, i) {
          if (item.abbreviation == (res.data.bankcard&&res.data.bankcard.bankcard[0].bank)) {
            index = i
          }
        })
        that.setData({
          name: res.data.bankcard&&res.data.bankcard.bankcard[0].name,
          cardnumber: res.data.bankcard&&res.data.bankcard.bankcard[0].cardnumber,
          index: index
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})